#include<stdio.h>

void initial(int *ptr, int *len)
{
	int i,j,n;

	printf("How many numbers do you want to insert? \n");
	scanf("%d", &n);

	for (i=0;i<n;i++)
	{
		scanf("%d", &ptr[i]);
	}

	printf("The list is: \n");

	for (j=0;j<n;j++)
	{
		printf("%d", ptr[j]);
	}

	*len = *len + n;
}




void append(int *ptr, int *len)
{
	int i;

	printf("Enter the element");
	scanf("%d", &ptr[*len]);

	*len = *len + 1;

	printf("The list is: \n");

	for (i=0;i<(*len);i++)
	{
		printf("%d", ptr[i]);
	}

}




void sort(int *ptr, int *len)
{
	int i,j,temp;
	
	for (i=0;i<(*len)-1;i++)
	{
		for (j=0;j<(*len)-1;j++)
		{
			if (ptr[j]>ptr[j+1])
			{
				temp=ptr[j];
				ptr[j]=ptr[j+1];
				ptr[j+1]=temp;
			}
		}
	}

	printf("The list is: \n");

	for (i=0;i<(*len);i++)
	{
		printf("%d", ptr[i]);
	}

}




void extend(int *ptr, int *len)
{
	int *ptr1;
	int *len1;
	int length1=0;

	ptr1 = (int *)malloc(1000*sizeof(int));
	
	len1 = &length1;

	int i,j,k,n;

	printf("How many numbers do you want to insert in the new list? \n");
	scanf("%d", &n);

	for (i=0;i<n;i++)
	{
		scanf("%d", &ptr1[i]);
	}

	printf("The list is: \n");

	for (j=0;j<n;j++)
	{
		printf("%d", ptr1[j]);
	}

	*len1 = *len1 + n;
	
	for (k=0;k<n;k++)
	{
		ptr[(*len)+k] = ptr1[k];
	}
	
	*len = *len + n;

	printf("The list is: \n");

	for (j=0;j<(*len);j++)
	{
		printf("%d", ptr[j]);
	}

}




void index(int *ptr, int *len)
{
	int i,n,a=0;	

	printf("Enter the element to be searched: \n");
	scanf("%d", &n);

	for (i=0;i<(*len);i++)
	{
		if (ptr[i] == n)
		{
			printf("It is present at the location %d. \n", i);
			a++;
			break;	
		}
	}
	if (a==0)
	{
		printf("It is not present in the list. \n");
	}
}




void reverse(int *ptr, int *len)
{
	int i,j,temp;
	
	for(i=0; i<(*len)/2; i++)
	{
		temp=ptr[i];
		ptr[i]=ptr[(*len)-1-i];
		ptr[(*len)-1-i]=temp;
	}

	printf("The reversed list is: \n");

	for (j=0;j<(*len);j++)
	{
		printf("%d", ptr[j]);
	}

} 




void pop(int *ptr, int *len)
{
	int i,j,n;	
	
	printf("Enter the index, the value at which you want to pop. \n" );
	scanf("%d", &n);

	ptr[n]=0;
	
	for (i=n;i<(*len);i++)
	{
		ptr[n] = ptr[n+1];
	}
	
	*len = *len - 1;	

	printf("The new list is: \n");

	for (j=0;j<(*len);j++)
	{
		printf("%d", ptr[j]);
	}
	
}




void delete(int *ptr, int *len)
{
	int a,i,j,n;

	printf("Enter the element you want to delete. ");
	scanf("%d", &n);

	for (i=0;i<(*len);i++)
	{
		if (ptr[i] == n)
		{
			a++;			
			ptr[n] = 0;
			for (i=n;i<(*len);i++)
			{
				ptr[n] = ptr[n+1];
			}
		}
	}

	if (a==0)
	{
		printf("Element not found.");
	}
	else
	{
		*len = *len - 1;	

		printf("The new list is: \n");

		for (j=0;j<(*len);j++)
		{
			printf("%d", ptr[j]);
		}
	}

}




void count(int *ptr, int *len)
{
	int a,i,n;	
	
	printf("Enter the element to be counted. ");
	scanf("%d", &n);

	a=0;
	
	for (i=0;i<(*len);i++)
	{
		if (ptr[i]==n)
		{	
			a++;
		}
	}	

	printf("The no. of times the element is there in the list is %d", a);

}




void insert(int *ptr, int *len)
{
	int n,i,j;
	
	printf("Enter the no. to be inserted: ");
	scanf("%d",&n);
	
	printf("Enter the position where u want this no. to be inserted:");
	scanf("%d",&i);

	*len = (*len)+1;

	for(j=(*len);j>i;j--)
	{
		ptr[j]=ptr[j-1];
	}
	ptr[i]=n;

	printf("The new list is: ");

	for(j=0;j<(*len);j++)
	{
		printf("%d", ptr[j]);
	}


}




void main()
{
	int *ptr;
	int *len;
	int length=0;
	int ans1;

	char ans='y';

	ptr = (int *)malloc(1000*sizeof(int));
	
	len = &length;

	while(ans=='y')
	{	
	
		printf("This is python list implementation through arrays.\n");
		printf("1. Initial list\n");
		printf("2. Index\n");
		printf("3. Count\n");
		printf("4. Insert\n");
		printf("5. Append\n");
		printf("6. Sort\n");
		printf("7. Extend\n");
		printf("8. Pop\n");
		printf("9. Remove\n");
		printf("10. Reverse\n");


		printf("Enter your choice: ");
		scanf("%d", &ans1);
	
		if (ans1==1)
		{
			initial(ptr, len);
		}
		else if(ans1==2)
		{
			index(ptr, len);
		}
		else if(ans1==3)
		{
			count(ptr, len);
		}
		else if(ans1==4)
		{
			insert(ptr, len);
		}
		else if(ans1==5)
		{
			append(ptr, len);
		}
		else if(ans1==6)
		{
			sort(ptr, len);
		}
		else if(ans1==7)
		{
			extend(ptr, len);
		}
		else if(ans1==8)
		{
			pop(ptr, len);
		}
		else if(ans1==9)
		{
			delete(ptr, len);
		}
		else if(ans1==10)
		{
			reverse(ptr, len);
		}
		else 
		{
			printf("Invalid selection.");
		}

		fflush(stdin);
		
		printf("Do you want to continue: ");
		scanf("%s", &ans);

	};
	
	
}
